home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / CHARTP10.ARJ / TREE.CPP < prev    next >
C/C++ Source or Header  |  1992-01-26  |  3KB  |  152 lines

  1.  
  2. // Copyright 1992, David Perelman-Hall & Jamshid Afshar
  3.  
  4. #include "tree.h"
  5. #include <string.h>
  6. #include <assert.h>
  7.  
  8.  
  9.  
  10. ostream& operator << ( ostream& os, const Tree& tree )
  11. {
  12.    assert(tree.isvalid());
  13.    if (tree._num_children > 0)
  14.       os << "(";
  15.    os << tree.cat();
  16.    for(int i = 0; i<tree._num_children; i++)
  17.       os <<  " " << *tree._children[i];
  18.    if (tree._num_children > 0)
  19.       os << ")";
  20.    return os;
  21. }
  22.  
  23.  
  24. //constructor
  25. Tree::Tree(const Category& cat)
  26.    :_num_children(0), _cat(cat)
  27. {
  28.    for(int i=0; i<MAX_CHILDREN; i++)
  29.       _children[i] = NULL;
  30. }
  31.  
  32.  
  33.  
  34. //coppy constructor
  35. Tree::Tree( const Tree &tree )
  36.    :_num_children(0)
  37. {
  38.    assert(this != &tree);
  39.    for(int i=0; i<MAX_CHILDREN; i++)
  40.       _children[i] = NULL;
  41.  
  42.    *this = tree;
  43. }
  44.  
  45.  
  46.  
  47. //operator =
  48. void Tree::operator = ( const Tree &tree )
  49. {
  50.    assert(this != NULL);
  51.    //empty current contents
  52.    clear();
  53.  
  54.    _cat = tree._cat;
  55.    _num_children = tree._num_children;
  56.    for(int i = 0; i<_num_children; i++){
  57.       assert(_children[i]==NULL);
  58.       assert(tree._children[i]!=NULL);
  59.       _children[i] = new Tree(*tree._children[i]);
  60.    }
  61.  
  62.    assert(isvalid());
  63. }
  64.       
  65. bool Tree::isvalid() const
  66. {
  67.    if (this == NULL)
  68.       return FALSE;
  69.     
  70.    if (_num_children < 0 || _num_children >= MAX_CHILDREN)
  71.       return FALSE;
  72. /*
  73.    if (strcmp(_cat, "") == 0)
  74.       return FALSE;
  75. */
  76.    {
  77.    for(int i = 0; i<_num_children; i++){
  78.       if (_children[i]==NULL)
  79.          return FALSE;
  80.    }
  81.    }
  82.    {
  83.    for(int i = _num_children; i<MAX_CHILDREN; i++){
  84.       if (_children[i]!=NULL)
  85.          return FALSE;
  86.    }
  87.    }
  88.    return TRUE;
  89. }
  90.  
  91. //operator ==
  92. bool Tree::operator == ( const Tree &tree ) const
  93. {
  94.    if( _cat != tree._cat )
  95.       return FALSE;
  96.  
  97.    if( _num_children != tree._num_children )
  98.       return FALSE;
  99.  
  100.    for(int i = 0; i<_num_children; i++){
  101.       assert(_children[i]!=NULL);
  102.       assert(tree._children[i]!=NULL);
  103.       if( *_children[i] != *tree._children[i] )
  104.          return FALSE;
  105.    }
  106.  
  107.    return TRUE;
  108. }
  109.  
  110. //destructor
  111. Tree::~Tree()
  112. {
  113.    clear();
  114. }
  115.  
  116.  
  117. //clear
  118. void Tree::clear()
  119. {
  120.    assert(isvalid());
  121.    for(int i = 0; i<_num_children; i++){
  122.       assert(_children[i]!=NULL);
  123.       delete _children[i]; //free up whatever children[i] pointed to
  124.       _children[i] = NULL;
  125.    }
  126.    _num_children = 0;
  127.    _cat.clear();
  128. }   
  129.  
  130.  
  131. void Tree::add_child(const Tree &tree )
  132. {
  133.    assert( _num_children < MAX_CHILDREN );
  134.    _children[_num_children] = new Tree( tree );
  135.    _num_children++;
  136. }
  137.  
  138.  
  139. //get children in current tree
  140. Category_Sequence Tree::get_children() const
  141. {
  142.    Category_Sequence CS;
  143.  
  144.    for(int i = 0; i<_num_children; i++) {
  145.       assert(_children[i]!=NULL);
  146.       CS += _children[i]->cat();
  147.       }
  148.  
  149.    return CS;
  150. }
  151.  
  152.